//
//  AudioVisualViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "AudioVisualViewController.h"
#import "BlockGetAFNYiBu.h"
#import "AudioVisualModel.h"
#import "AudioVisualCollectionViewCell.h"
#import "AudioVisualDetailViewController.h"
#import <MJRefresh.h>
#import "BaseCollectionView.h"
@interface AudioVisualViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
// 记录数据
@property (nonatomic, retain)NSMutableArray * allArray;

@property (nonatomic, retain)BaseCollectionView * collectionView;
// 判断下拉, 上拉状态
@property (nonatomic, assign)BOOL upLoding;
// 记录页数
@property (nonatomic, assign)NSInteger currentPage;
@end

@implementation AudioVisualViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.allArray = [NSMutableArray array];
    //[self setTitle:@"家具电台"];
    [self addTabBar];
    // 下拉刷新
    [self headerRequest];
    // 上拉加载
    [self footerRequest];
    [self.collectionView registerClass:[AudioVisualCollectionViewCell class] forCellWithReuseIdentifier:@"coll"];
    
}
-(void)addTabBar
{
    //自定义UINavigationBar的标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50*WIDTHSCALE, 0, 150*WIDTHSCALE, 44*HEIGHTSCALE)];
    titleLabel.text = @"家居电台";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:20];
    self.navigationItem.titleView = titleLabel;
    
    
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setSectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
        _collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 44) collectionViewLayout:flowLayout];
        [_collectionView setDelegate:self];
        [_collectionView setDataSource:self];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}
// 下拉刷新
- (void)headerRequest
{
    __block AudioVisualViewController * VC = self;
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        VC.upLoding = NO;
        VC.currentPage = 1;
        [VC requestData];
    }];
    [self.collectionView.header beginRefreshing];
}
// 上拉加载
- (void)footerRequest
{
    __block AudioVisualViewController *VC = self;
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        VC.upLoding = YES;
        VC.currentPage++;
        [VC requestData];
    }];
}
- (void)requestData
{
//    NSString * url = @"http://search.ximalaya.com/front/v1?condition=recent&core=track&device=iPhone&kw=家居&page=1&rows=20";
    if (self.upLoding == NO) {
        [self.allArray removeAllObjects];
    }
    NSString * str = @"%E5%AE%B6%E5%B1%85";
    NSString * url = [NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?condition=recent&core=track&device=iPhone&kw=%@&page=%ld&rows=20", str, self.currentPage];
    [BlockGetAFNYiBu GetAfnURLWithString:url resultBlock:^(id result) {
        NSArray * array = [[result objectForKey:@"response"] objectForKey:@"docs"];
        for (NSDictionary * dic in array) {
            AudioVisualModel * model = [[AudioVisualModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:model];
        }
        [self.collectionView reloadData];
        // 结束刷新状态
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
    } errorBlcok:^(NSError *error) {
        NSLog(@"%@", error);
        
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AudioVisualCollectionViewCell * coll = [collectionView dequeueReusableCellWithReuseIdentifier:@"coll" forIndexPath:indexPath];
    [coll setBackgroundColor:[UIColor whiteColor]];
    AudioVisualModel * model = [self.allArray objectAtIndex:indexPath.row];
    coll.backgroundColor = [UIColor wheatColor];
    coll.model = model;
    return coll;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(WIDTH - 20, 0.225 * HEIGHT);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AudioVisualModel * model = [self.allArray objectAtIndex:indexPath.row];
    AudioVisualDetailViewController * AuDetail = [AudioVisualDetailViewController shareAudioVisualDetailViewComtroller];
    [AuDetail setHidesBottomBarWhenPushed:YES];
    AuDetail.model = model;
    AuDetail.songID = model.play_path_64;
    [self.navigationController pushViewController:AuDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
